function [nFs, mXY, vShut, bReload] = Filled_Circle(bAutoLoad)
% GalvoScanControl
% Scan script template: Filled Circle

% Scan scripts should return four variables (in this order):
%  nFs      The sample rate (1/sec between scan points)
%  mXY      A matrix with two columns in the format [X Y], where
%               X  are x-positions
%               Y  are y-positions
%           The unit of this matrix is millimeters, relative to the origin
%  vShut    Vector with same length as mXY where values indicate shutter
%           open/close state (when a shutter is controlled) or the beam
%           intensity (when the output is directed to an intensity
%           modulated laser module, e.g. Coherent CUBE).
%           Note: Values of vShut should not exceed +/- 5V. Values beyond
%           these limits will be fixed at +/- 5V by the software.
%  bReload  Boolean indicating whether the script should be re-loaded each
%           time before executing. Setting this to true (1) will decrease
%           the repetition rate and should only be used for long programs
%           that are not executed at high frequency.



% Scan along a circular expanding trajectory. Useful for simulating large
% spot size if trajectory is scanned very fast.


persistent sPnts sTurns sDur sR1 sR2
bReload = 0;

% Default values
if isempty(sPnts), sPnts = '100'; end
if isempty(sTurns), sTurns = '5'; end
if isempty(sDur), sDur = '1000'; end
if isempty(sR1), sR1 = '0'; end
if isempty(sR2), sR2 = '1'; end

cAnswers = inputdlg({'Number of dwell points', ...
    'Turns', ...
    'Duration (ms)', ...
    'Start radius (mm)', ...
    'End radius (mm)'} , ...
    'Rectangle', ones(1, 5), ...
    {sPnts sTurns sDur sR1 sR2} );
if isempty(cAnswers), return, end

% For some reason, closing the input dialog takes forever.
% Placing this pause here prevents this.
pause(0.1)

sPnts = cAnswers{1};
sTurns = cAnswers{2};
sDur = cAnswers{3};
sR1 = cAnswers{4};
sR2 = cAnswers{5};


% Generate a circle with varying radius
nPnts = str2num(sPnts); % number of dwell points
r1 = str2num(sR1); % start radius
r2 = str2num(sR2); % end radius
nDur = str2num(sDur) / 1000; % duration, sec
nTurns = str2num(sTurns);

% Computer other parameters
nShutVolt = 5;
rr = linspace(r1,r2,nPnts);
theta = linspace(0, (2*pi*nTurns), nPnts);
x = rr .* cos(theta);
y = rr .* sin(theta);

% t is the cumulative arclength along the edges of the polygon.
t = cumsum(sqrt([0,diff(x(:)').^2] + [0,diff(y(:)').^2]));

% The total distance around the polygon is t(end)
tmax = t(end);

% create a piecewise linear spline for each of px and py,
% as a function of the cumulative chordwise arclength.
splx = mkpp(t,[diff(x(:))./diff(t'),x(1:(end-1))']);
sply = mkpp(t,[diff(y(:))./diff(t'),y(1:(end-1))']);

% now interpolate the polygon splines, splx and sply.
% Nt is the number of points to generate around the
% polygon. The first and last points should be replicates
% at least to within floating point trash.)
Nt = nPnts;
tint = linspace(0,tmax,Nt);
qx = ppval(splx,tint);
qy = ppval(sply,tint);

mXY = [qx(:) qy(:)];

% compute duration, in samples, for shutter to open
nShutOpenDur = 0.7; % ms
nPreShutDur = ceil((nShutOpenDur/1000) / (nDur / nPnts));

vShut = [ones(nPreShutDur,1); ones(size(mXY,1),1); 0].*nShutVolt; % shutter always open but starts/stops closed
mXY = [repmat(mXY(1,:),nPreShutDur,1); mXY; mXY(end, :)];
nFs = 1 / (nDur / nPnts); % 1/sec, Hz

return

